/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.network;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import twaver.EditableFilter;
import twaver.Element;
import twaver.ElementLabelEditor;
import twaver.Equipment;
import twaver.MovableFilter;
import twaver.ResizableFilter;
import twaver.ResizableNode;
import twaver.ShapeLink;
import twaver.ShapeNode;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.LayerManagerPane;
import twaver.network.NetworkToolBarFactory;
import twaver.network.TNetwork;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import twaver.table.TTableNavigator;
import twaver.table.TTreeTable;
import twaver.tree.TTree;
import demo.DemoPane;
import demo.DemoUtil;
import demo.editor.EditablePopupMenuGenerator;
import demo.tree.TreePopupMenuGenerator;

public class NetworkEditorDemo extends DemoPane {
	
    private TDataBox box = new TDataBox("Network Editor");
    private TNetwork network = new TNetwork(box);
    private TTree tree = new TTree(box);
    private JSplitPane mainSplit = new JSplitPane();
    private JSplitPane topSplit = new JSplitPane(); 
    private JPanel leftPane = new JPanel(new BorderLayout());
    private JPanel rightPane = new JPanel(new BorderLayout());
    private JSplitPane leftSplit = new JSplitPane();
    private PalettePane palettePane = new PalettePane(network);
    private TPropertySheet sheet = new TPropertySheet(box);
    private TTreeTable table = new TTreeTable(box); 
    private LayerManagerPane layerManagerPane = new LayerManagerPane(network);
    private JPanel bottomPane = new JPanel(new BorderLayout());
    private JButton dockButton = new JButton(TWaverUtil.getImageIcon("/demo/resource/images/dock.png"));
    private JDialog dockDialog = null;
    private JCheckBox editMode = new JCheckBox("Edit Mode", true);

    public NetworkEditorDemo() {    	
    	initTablePane();
        TPropertySheetPane sheetPane = new TPropertySheetPane(sheet);
    	
        JToolBar toolbar = NetworkToolBarFactory.getToolBar(TWaverConst.EDITOR_TOOLBAR, network);
        this.add(toolbar, BorderLayout.NORTH);
        toolbar.addSeparator();
        toolbar.add(editMode);		
		editMode.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				network.getCanvas().repaint();
				network.setLinkBundleAlternate(editMode.isSelected());
			}
		});
        this.network.setToolbar(null);
        this.network.setFullScreenAttachment(toolbar, this, BorderLayout.NORTH);
        
        mainSplit.setOneTouchExpandable(true);
        mainSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
        leftSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
        leftSplit.setContinuousLayout(true);
        topSplit.setContinuousLayout(true);
        this.add(mainSplit, BorderLayout.CENTER);
        
        mainSplit.add(topSplit, JSplitPane.TOP);
        mainSplit.add(bottomPane, JSplitPane.BOTTOM);
        topSplit.add(leftPane, JSplitPane.LEFT);
        leftPane.add(leftSplit, BorderLayout.CENTER);
        topSplit.add(rightPane, JSplitPane.RIGHT);
        rightPane.add(network, BorderLayout.CENTER);
        rightPane.add(palettePane, BorderLayout.NORTH);
        leftSplit.add(new JScrollPane(tree), JSplitPane.TOP);
        leftSplit.add(sheetPane, JSplitPane.BOTTOM);
        leftSplit.setDividerLocation(200);
        topSplit.setDividerLocation(200);
        mainSplit.setDividerLocation(450);

        // set network
    	network.setEnableUndoRedoWithKeyboard(true);
        network.setEnsureVisibleOnSelected(true);
		network.setEnableDeleteWithKeyboard(true);
		network.setEnableCopyPasteWithKeyboard(true);

		network.setElementLabelEditableFilter(new EditableFilter(){
			public boolean isEditable(Element element) {
				return editMode.isSelected();
			}
		});
		network.setMovableFilter(new MovableFilter(){
			public boolean isMovable(Element element) {
				if(editMode.isSelected()){
					return true;
				}
				return !(element instanceof Equipment);
			}
		});
        network.setResizableFilter(new ResizableFilter(){
			public boolean isResizable(Element element) {
				if(!editMode.isSelected()){
					return false;
				}
				return element instanceof ResizableNode || 
						element instanceof ShapeLink || 
						element instanceof ShapeNode;
			}
		});
        network.setPopupMenuGenerator(new EditablePopupMenuGenerator(network));
        
        JTextField inputField = network.getElementLabelInputField();
		inputField.setOpaque(true);
		inputField.setBackground(new Color(100, 100, 180));
		inputField.setForeground(Color.YELLOW);
		inputField.setFont(new Font("default", Font.ITALIC, 18));
		inputField.setSelectedTextColor(Color.RED);
		inputField.setCaretColor(Color.WHITE);

        // set tree
        tree.setEnsureVisibleOnSelected(true);
        tree.setElementDraggable(true);
        tree.setPopupMenuGenerator(new TreePopupMenuGenerator(tree));
        tree.setDataBoxLabelEditable(false);
        tree.setElementLabelEditableFilter(new EditableFilter(){
			public boolean isEditable(Element element) {
				return true;
			}
        });
        
        tree.setElementLabelEditor(new ElementLabelEditor(){
			public void setLabel(Element element, String newLabel) {
				// defaultly the new label is setted to the name of element,
				// you can customize to other property of the element.
				element.setName(newLabel);
			}
        });
        // set propertySheet
        sheet.setEditable(true);
        
        try {
			box.parse("/demo/editor/network/Network.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
    	box.getUndoRedoManager().setLimit(200);  
    	
    	
    }
    
    private void initTablePane(){
    	final JComboBox elementClassSelection = this.palettePane.createComboBox();    	
    	elementClassSelection.setSelectedItem(Element.class);
    	table.setElementClass(Element.class);
    	table.setEditable(false);
//    	table.getTree().setRootVisible(false);
    	table.setIteratorByHiberarchy(true);
    	
    	elementClassSelection.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Object item = elementClassSelection.getSelectedItem();
				if(item instanceof Class){
					table.setElementClass((Class)item);
				}else{
					table.setElementClass(null);
				}
			}
    	});
    	
    	final JCheckBox editable = new JCheckBox("Cell Editable", table.isEditable());
    	editable.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.setEditable(editable.isSelected());
			}
    	});
    	
    	JPanel topPane = new JPanel(new BorderLayout());
    	topPane.add(elementClassSelection, BorderLayout.WEST);
    	topPane.add(editable, BorderLayout.EAST);
    	
    	JPanel statusPane = new JPanel(new BorderLayout());
    	statusPane.add(DemoUtil.createStatusLabel(network), BorderLayout.WEST);
    	statusPane.add(dockButton, BorderLayout.EAST);  
    	
    	JPanel tablePane = new JPanel(new BorderLayout());
    	tablePane.add(topPane, BorderLayout.NORTH);
    	tablePane.add(new JScrollPane(table), BorderLayout.CENTER); 
    	TTableNavigator navigator = new TTableNavigator(table.getTableModel(), new int[] {20, 30, 50, 0}, true);
    	tablePane.add(navigator, BorderLayout.SOUTH); 
    	
    	JTabbedPane tabPane = new JTabbedPane();
    	tabPane.add("Tree Table", tablePane);
    	tabPane.add("Layer Manager", this.layerManagerPane);
    	
    	bottomPane.add(tabPane, BorderLayout.CENTER); 
    	bottomPane.add(statusPane, BorderLayout.SOUTH);  
    	
		dockButton.setBorder(BorderFactory.createEtchedBorder());
		dockButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {				
				if(dockDialog == null){
			    	Frame parent = (Frame)TWaverUtil.getWindowForComponent(NetworkEditorDemo.this);
			    	dockDialog = new JDialog(parent){
			    	    protected void processWindowEvent(WindowEvent e) {
			    	    	if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			    	    		dockTable();
			    	    		return;
			    	    	}
			    	        super.processWindowEvent(e);
			    	    }
			    	};
			    	dockDialog.setModal(false);
			    	dockDialog.setVisible(false);
			    	dockDialog.setResizable(true);
			    	dockDialog.setBounds(50, 50, 550, 300);
			    	dockDialog.getContentPane().setLayout(new BorderLayout());
				}
				dockTable();
			}
		});
    }
    
    private void dockTable(){
		if(mainSplit.getBottomComponent() == null){
			dockDialog.getContentPane().remove(bottomPane);
			mainSplit.add(bottomPane, JSplitPane.BOTTOM);
			Integer position = (Integer)mainSplit.getClientProperty("position");
			mainSplit.setDividerLocation(position.intValue());
			dockDialog.setVisible(false);
		}else{
			mainSplit.putClientProperty("position", TWaverUtil.valueOf(mainSplit.getDividerLocation()));
			mainSplit.remove(bottomPane);
			dockDialog.getContentPane().add(bottomPane);
			dockDialog.setVisible(true);
		}
    }


}